<template>
  <div class='xtx-goods-page' v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{ goods.categories[1].name }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{ goods.categories[0].name }}</XtxBreadItem>
        <XtxBreadItem to="/">{{goods.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <XtxImageView :image-list="goods.mainPictures" />
          <GoodsSale :goods='goods'/>
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
            <GoodsInfo :goods='goods'/>
              <!-- 规格选择组件 -->
          <XtxGoodSku :goods="goods" @change="goodSkuChangeFn"  />
          <!-- 计数组件  最大数量不能超过库存-->
          <XtxNumbox v-model="count" :min="1" :max="goods.inventory"></XtxNumbox>
          <!-- 按钮 -->
          <XtxButton @click="addToCart" type='primary' style="margin-top:20px">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsDetail :goods='goods'/>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :goodsId='goods.id' :type='1'/>
          <GoodsHot :goodsId='goods.id' :type='2'/>
          <GoodsHot :goodsId='goods.id' :type='3'/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoodsDetail } from '@/api/goods'
import { useRoute } from 'vue-router'
import { ref } from 'vue'
import GoodsSale from '@/views/Goods/components/goods-sale.vue'
import GoodsInfo from '@/views/Goods/components/goods-info.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import { useStore } from 'vuex'
import { message } from '@/components/Message'
export default {
  components: { GoodsSale, GoodsInfo, GoodsDetail, GoodsHot },
  name: 'XtxGoodsPage',
  setup () {
    const count = ref(1) // 加入购物车的数量
    const route = useRoute()
    const goods = ref(null)
    findGoodsDetail(route.params.id).then(({ result }) => {
      // console.log(result)
      goods.value = result
    })

    const goodSkuChangeFn = (val) => {
      // console.log('商品详情', goods.value)
      // console.log('规格选择组件子传父结果', val)
      // 如果选择了规格，才更新商品信息
      if (val.skuId) {
        // goods.value.price = val.price
        // goods.value.oldPrice = val.oldPrice
        // goods.value.inventory = val.inventory
        // goods.value.skuId = val.skuId
        // goods.value.specsText = val.specsText
        // 🔔 通过遍历对象化简代码
        for (const key in val) {
          goods.value[key] = val[key]
        }
      }
    }

    const store = useStore()

    // 点击加入购物车
    const addToCart = () => {
      // 1. 必须选择规格
      if (!goods.value.skuId) return message({ text: '请选择规格' })
      // 2. 添加的数量不能超过库存数
      if (count.value > goods.value.inventory) return message({ text: '库存超出了' })
      // 3. 收集商品信息 - 💥 注意：后端接口字段命名不规范，要注意字段名匹配
      const goodsObj = {
        id: goods.value.id, // 商品id
        name: goods.value.name, // 商品名称
        picture: goods.value.mainPictures[0], // 商品图片
        skuId: goods.value.skuId, // 有效skuId
        price: goods.value.oldPrice, // 原价格
        nowPrice: goods.value.price * 10, // 现价格  // 现价格 - 💥人为制造错误的价格，用于测试商品价格更新
        attrsText: goods.value.specsText, // 规格文案
        stock: goods.value.inventory, // 库存
        isEffective: true, // 是否有效商品
        selected: true, // 是否选中
        count: count.value // 商品数量
      }
      // console.log('后端返回的', goods.value)
      // console.log('加入购物城用的', goodsObj)

      // 调用vuex
      store.dispatch('cart/asyncInertCart', goodsObj).then(() => {
        // 加入成功 提示
        message({ type: 'success', text: '加入购物车成功' })
        goods.value.inventory -= count.value
      })
    }

    return { goods, goodSkuChangeFn, count, addToCart }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
   display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;

}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
